package com.gis.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import com.gis.service.GeoServerRestApi;

@Configuration
public class GeoServerRestApiConfig {
    @Value("${gis.geoserver.workspace}")
    private String workspace;

    @Value("${gis.geoserver.geoserver_url}")
    private String baseUrl;

    @Value("${gis.geoserver.username}")
    private String username;
    @Value("${gis.geoserver.password}")
    private String password;

    @Bean
    public GeoServerRestApi geoServerRestApi() {
        // TODO: baseUrl不要用硬编码，应该从配置文件中读取
        RestClient restClient = RestClient.builder()
                .baseUrl(baseUrl + "/rest/workspaces/" + workspace)
                .defaultHeaders(headers -> headers.setBasicAuth(username, password))
                .build();
        RestClientAdapter adapter = RestClientAdapter.create(restClient);
        HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter)
                .build();
        return factory.createClient(GeoServerRestApi.class);
    }
}
